package com.smart_environmental_protection.controller;

import com.smart_environmental_protection.pojo.User;
import com.smart_environmental_protection.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

@RestController
public class UserController {
    @Autowired
    private UserService userService;


    @PostMapping(value = {"/loginWithoutCode"})
    public Map<String,Object> login(@RequestBody User user){
        Map map=new HashMap<String,Object>();
        User user1=userService.login(user);

        if (user1!=null){ map.put("success",true);
            map.put("msg","成功");
            map.put("code",200);
            map.put("data",user1);
        }else {
            map.put("success",false);
            map.put("msg","失败");
        }
        return map;
    }




    @PostMapping(value = {"/system/user/profile/updatePwd"})
    public Map<String,Object> UpdatePws(@RequestBody User user){
        Map map=new HashMap<String,Object>();
        int user1=userService.UpdatePws(user);
        if (user1>0){
            map.put("success",true);
            map.put("msg","成功");
            map.put("code",200);
        }else {
            map.put("success",false);
            map.put("msg","失败");
        }
        return map;
    }






    @PostMapping(value = {"/system/user"})
    public Map<String,Object> userLogin(@RequestBody User user){
        Map map=new HashMap<String,Object>();
        User user1=userService.Userlogin(user);
        Random r = new Random();
        int result=r.nextInt((999999 - 100000) + 1) + 100000;
        long Times=System.currentTimeMillis();
        if (user1!=null){ map.put("success",true);
            map.put("msg","成功");
            map.put("code",200);
            map.put("codeNum",result);
            map.put("time",Times);
            map.put("data",user1);
        }else {
            map.put("success",false);
            map.put("msg","失败");
        }
        return map;
    }

@GetMapping(value = {"/system/otp/generate"})
public Map<String,Object> InsertUser(){
    Map map=new HashMap<String,Object>();
    Random r = new Random();
    int result=r.nextInt((999999 - 100000) + 1) + 100000;
    map.put("success",true);
    map.put("msg","成功");
    map.put("code",200);
    map.put("codeNum",result);
    return map;
}





    @PostMapping(value = {"/user/register"})
    public Map<String,Object> InsertUser(@RequestBody User user){
        Map map=new HashMap<String,Object>();
        int  user1=userService.InsertUser(user);
        if (user1==-1){
            map.put("success",false);
            map.put("msg","该用户已注册");
        }else if (user1>0){ map.put("success",true);
            map.put("msg","成功");
            map.put("code",200);
        }else {
            map.put("success",false);
            map.put("msg","失败");
        }
        return map;
    }

    @PutMapping(value = {"/system/user/profile/updateMobile"})
    public Map<String,Object> UpdatePhone(@RequestBody User user){
        Map map=new HashMap<String,Object>();
        int  user1=userService.UpdatePhone(user);
        if (user1==-1){
            map.put("success",false);
            map.put("msg","该用户已注册");
        }else if (user1>0){ map.put("success",true);
            map.put("msg","成功");
            map.put("code",200);
        }else {
            map.put("success",false);
            map.put("msg","失败");
        }
        return map;
    }

    @PostMapping(value = {"/mobileLogin"})
    public Map<String,Object> UpdateUser(@RequestBody User user){
        Map map=new HashMap<String,Object>();
        int res=userService.UpdateUser(user);
        if (res>0){ map.put("success",true);
            map.put("msg","成功");
            map.put("code",200);
        }else {
            map.put("success",false);
            map.put("msg","失败");
        }
        return map;
    }


    @GetMapping(value = {"/system/user/{id}"})
    public Map<String,Object> GetUsrId(@PathVariable Integer id){
        Map map=new HashMap<String,Object>();
        User res=userService.getUserById(id);
        if (res!=null){ map.put("success",true);
            map.put("msg","成功");
            map.put("code",200);
            map.put("data",res);
        }else {
            map.put("success",false);
            map.put("msg","失败");
        }
        return map;
    }




    @PostMapping({"/user/uploadheadicon"})
    @ResponseBody
    public Map<String, Object> getUserById(@RequestParam("file") MultipartFile uploadFile, @RequestParam("id") Integer id, HttpServletRequest req) {
        Map<String, Object> map = new HashMap();
        File dir = new File("D:\\uploadFiles");
        if (!dir.exists()) {
            dir.mkdirs();
        }

        try {
            String oldName = uploadFile.getOriginalFilename();
            String icon = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."), oldName.length());
            uploadFile.transferTo(new File(dir.getAbsolutePath() + File.separator + icon));
            icon = "/" + icon;
            User user = this.userService.getUserById(id);
            user.setIcon(icon);
            this.userService.updateIcon(user);
            map.put("success", true);
            map.put("msg", "成功");
            map.put("headiconpath", icon);
            return map;
        } catch (IOException var9) {
            map.put("success", false);
            map.put("msg", "失败");
            return map;
        }
    }




}
